#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct S{
	string s;
	int y = 0;
	int m = 0;
	int d = 0;
	int count = 0;
}arr[110];
bool cmp(struct S s1,struct S s2){
	if(s1.y != s2.y)
	{
		return s1.y < s2.y;
	}
	else if(s1.y == s2.y)
	{ 
		if(s1.m != s2.m){
			return s1.m < s2.m;
		}
		else if(s1.m == s2.m)
		{ 
			if(s1.d != s2.d){
				return s1.d < s2.d;
			}
			else if(s1.d == s2.d)
			{	
				if(s1.count > s2.count) 
				{
					return 1;
				}
				else return 0;
			}
		}
	}
}
int main(){
	int n,h,k,p;
	string s,s1,s2;
	cin >> n;
	for(int i = 0 ; i < n; i++){
		cin >> arr[i].s >> arr[i].y >> arr[i].m >> arr[i].d;
		arr[i].count = i;
	}
	sort(arr, arr + n, cmp); 
	for(int i = 0; i < n; i++){ 
		cout <<  arr[i].s  << endl;
	}
	return 0;
} 
